package net.kunyv.base.controller;

import net.kunyv.base.entity.SysCompany;
import net.kunyv.base.entity.SysUser;
import net.kunyv.base.service.CorpManageService;
import net.kunyv.base.service.SysCodeService;
import net.kunyv.util.PageParam;
import net.kunyv.util.PageResult;
import net.kunyv.util.SortParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * @author sunyb
 * @create 2020-07-07 15:57
 */
@Controller
@RequestMapping(value = "/Company")
public class CompanyManageController {
    @Autowired
    private SysCodeService sysCodeService;
    @Autowired
    private CorpManageService corpManageService;

    @RequiresPermissions("sysCode:query")
    @RequestMapping("/query")
    public String query() {
        return "login/system/corp_manage";
    }

    /**
     * 代码表维护功能获取datagrid数据
     *
     * @param fldEng
     * @param code
     * @param pageParam
     * @return net.kunyv.bde.ssm.PageResult
     * @throw
     * @author sunyb
     * @date 2017/8/3
     */
    @ResponseBody
    @RequiresPermissions("sysCode:query")
    @RequestMapping(method = RequestMethod.GET, value = "/getSysCodeData")
    public PageResult getSysCodeData(String fldEng, String code, PageParam pageParam, SortParam sortParam) {
//        return sysCodeService.getSysCodeGridData(fldEng, code, pageParam, sortParam);
        return corpManageService.getSysCodeGridData(fldEng, code, pageParam, sortParam);
    }

    /**
     * 获取代码类别下拉数据
     *
     * @param q
     * @return List<HashMap>
     * @throw
     * @author sunyb
     * @date 2020/7/10
     */
    @ResponseBody
    @RequiresPermissions("sysCode:query")
    @RequestMapping(value = "/getSysCodeList")
    public List<HashMap> getSysCodeList(String q) {
        return corpManageService.getSysCodeList(q);
    }

    /**
     * 新增或编辑
     *
     * @param sysCompany
     * @return java.lang.String
     * @throw
     * @author sunyb
     * @date 2020/7/7
     */
    @ResponseBody
    @RequiresPermissions(value = {"sysCode:add", "sysCode:edit"}, logical = Logical.OR)
    @RequestMapping(method = RequestMethod.POST, value = "/save")
    public String save(SysCompany sysCompany) {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
        return corpManageService.save(sysCompany, loginUser.getUserId());
    }

    /**
     * 删除
     *
     * @param id
     * @return java.lang.String
     * @throw
     * @author sunyb
     * @date 2017/8/7
     */
    @ResponseBody
    @RequiresPermissions("sysCode:delete")
    @RequestMapping(method = RequestMethod.POST, value = "/delete")
    public String delete(String id) {

        int i = corpManageService.deleteByPrimaryKey(id);
        if (i > 0) {
            return "ok";
        } else {
            return "删除失败";
        }
    }
}
